package com.jlwl.houseservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.entity.ServiceRequest;
import com.jlwl.houseservice.service.ServiceRequestService;
import com.jlwl.houseservice.vo.ServiceRequestCreateVO;
import com.jlwl.houseservice.vo.ServiceRequestQueryVO;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 服务需求控制器
 */
@RestController
@RequestMapping("/service-requests")
public class ServiceRequestController {

    @Autowired
    private ServiceRequestService serviceRequestService;

    /**
     * 创建服务需求
     */
    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public Result<Void> create(@Valid @RequestBody ServiceRequestCreateVO createVO) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        // 从用户名中提取用户ID（实际项目中应该从UserDetails中获取）
        Integer userId = getUserIdFromUsername(username);
        
        // 创建服务需求
        boolean success = serviceRequestService.createServiceRequest(createVO, userId);
        if (!success) {
            return Result.error("创建服务需求失败");
        }
        
        return Result.success();
    }

    /**
     * 获取服务需求列表
     */
    @GetMapping
    @PreAuthorize("hasRole('USER')")
    public Result<Page<ServiceRequest>> list(ServiceRequestQueryVO queryVO) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        // 从用户名中提取用户ID（实际项目中应该从UserDetails中获取）
        Integer userId = getUserIdFromUsername(username);
        
        // 查询服务需求列表
        Page<ServiceRequest> page = serviceRequestService.pageUserList(queryVO, userId);
        
        return Result.success(page);
    }

    /**
     * 获取服务需求详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public Result<ServiceRequest> getDetail(@PathVariable Integer id) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        
        // 从用户名中提取用户ID（实际项目中应该从UserDetails中获取）
        Integer userId = getUserIdFromUsername(username);
        
        // 获取服务需求详情
        ServiceRequest serviceRequest = serviceRequestService.getUserServiceRequestDetail(id, userId);
        if (serviceRequest == null) {
            return Result.error("服务需求不存在");
        }
        
        return Result.success(serviceRequest);
    }

    /**
     * 从用户名中提取用户ID
     * 实际项目中应该从UserDetails中获取
     */
    private Integer getUserIdFromUsername(String username) {
        // 这里简化处理，实际项目中应该从UserDetails中获取
        return 1;
    }
} 